﻿using System;
using System.Collections.Generic;
using System.Linq;
using FitNesseRunner;
using FitNesseRunner.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FitNesseRunnerTests
{
	[TestClass]
	public class ImportTableShould
	{
		[TestMethod]
		public void Empty()
		{
			var imports = GetImports(new[] { "import" });
			CollectionAssertEx.IsEmpty(imports);
		}

		[TestMethod]
		public void SingleImport()
		{
			var imports = GetImports(new[] {"import"},
			                         new[] {"namespace"});
			Assert.AreEqual("namespace", imports.Single());
		}

		[TestMethod]
		public void MultipleImports()
		{
			var imports = GetImports(new[] {"import"},
			                         new[] {"namespace1"},
			                         new[] {"namespace2"});
			CollectionAssertEx.AreEquivallent(new[] {"namespace1", "namespace2"}, imports);
		}

		private static IEnumerable<string> GetImports(params string[][] cells)
		{
			var importTable = new ImportTable(cells);
			var reflectionProviderDouble = new ReflectionProviderDouble();
			var context = new ContextDouble(reflectionProviderDouble);
			importTable.ExecuteAll(context);
			return reflectionProviderDouble.Imports;
		}

		private class ReflectionProviderDouble
		{
			public ReflectionProviderDouble()
			{
				_imports = new List<string>();
			}

			private readonly List<string> _imports;
			public IEnumerable<string> Imports
			{
				get { return _imports; }
			}

			public void AddImport(IEnumerable<string> imports)
			{
				_imports.AddRange(imports);
			}
		}

		private class ContextDouble : IContext
		{
			private readonly ReflectionProviderDouble _reflectionProviderDouble;
			private readonly List<string> _imports = new List<string>();

			public ContextDouble(ReflectionProviderDouble reflectionProviderDouble)
			{
				_reflectionProviderDouble = reflectionProviderDouble;
			}

			public object LastScriptTableObject { get; set; }

			public IReflectionProvider ReflectionProvider
			{
				get { throw new NotImplementedException(); }
			}

			public IDictionary<string, string> Variables
			{
				get { throw new NotImplementedException(); }
			}

			public void AddImports(IEnumerable<string> imports)
			{
				_reflectionProviderDouble.AddImport(imports);
			}
		}
	}
}
